********
* DATA *
********

* LISS DATA AVAILABLE FROM LISSDATA.NL, "POLITICS AND VALUES", WAVES 1-7

* Subjects and panels
gen id=_n

* Missings
recode cv08a013 cv08a014 cv08a017 cv08a018 cv09b013 cv09b014 cv09b017 cv09b018 cv10c013 cv10c014 cv10c017 cv10c018 cv11d013 cv11d014 cv11d017 cv11d018 cv12e013 cv12e014 cv12e017 cv12e018 cv13f013 cv13f014 cv13f017 cv13f018 cv14g013 cv14g014 cv14g017 cv14g018 (999=.)

* Immigration views
alpha cv08a116 cv08a117 cv08a118 cv08a119 cv08a120 cv08a121 cv08a122 cv08a123, item gen(nativism5) // 1 = end 2007/ start 2008	(Balkenende-3)
alpha cv09b116 cv09b117 cv09b118 cv09b119 cv09b120 cv09b121 cv09b122 cv09b123, item gen(nativism6) // 2 = end 2008				(Balkenende-3)
alpha cv10c116 cv10c117 cv10c118 cv10c119 cv10c120 cv10c121 cv10c122 cv10c123, item gen(nativism7) // 3 = end 2009				(Balkenende-3)
alpha cv11d116 cv11d117 cv11d118 cv11d119 cv11d120 cv11d121 cv11d122 cv11d123, item gen(nativism8) // 4 = end 2010				(Rutte-1)
alpha cv12e116 cv12e117 cv12e118 cv12e119 cv12e120 cv12e121 cv12e122 cv12e123, item gen(nativism9) // 5 = end 2011				(Rutte-1)
alpha cv13f116 cv13f117 cv13f118 cv13f119 cv13f120 cv13f121 cv13f122 cv13f123, item gen(nativism10) // 6 = end 2012				(Rutte-2)
alpha cv14g116 cv14g117 cv14g118 cv14g119 cv14g120 cv14g121 cv14g122 cv14g123, item gen(nativism11) // 7 = end 2013				(Rutte-2)


foreach var of varlist nativism5 nativism6 nativism7 nativism8 nativism9 nativism10 nativism11 {
	sum `var'
	gen `var'_b = `var' - r(min)
	sum `var'_b
	gen `var'_c = `var'_b / r(max)
	replace `var' = `var'_c
	replace `var' = 1-`var'
	}
	
	sum nativism5 nativism6 nativism7 nativism8 nativism9 nativism10 nativism11
	
* Protest 
alpha cv08a014 cv08a017 cv08a018, item gen(protest5)
alpha cv09b014 cv09b017 cv09b018, item gen(protest6)
alpha cv10c014 cv10c017 cv10c018, item gen(protest7)
alpha cv11d014 cv11d017 cv11d018, item gen(protest8)
alpha cv12e014 cv12e017 cv12e018, item gen(protest9)
alpha cv13f014 cv13f017 cv13f018, item gen(protest10)
alpha cv14g014 cv14g017 cv14g018, item gen(protest11)

rename (cv08a001 cv09b001 cv10c001 cv11d001 cv12e001 cv13f001 cv14g001) (govsat5 govsat6 govsat7 govsat8 govsat9 govsat10 govsat11)

rename (cv08a047 cv09b047 cv10c047 cv11d047 cv12e047 cv13f047 cv14g047) (exteff15 exteff16 exteff17 exteff18 exteff19 exteff110 exteff111)
rename (cv08a049 cv09b049 cv10c049 cv11d049 cv12e049 cv13f049 cv14g049) (exteff25 exteff26 exteff27 exteff28 exteff29 exteff210 exteff211)

* Vote
recode cv08a058 (12=1) (999=0) (nonmissing=0), gen(PVVvote5)
recode cv09b058 (12=1) (999=0) (nonmissing=0), gen(PVVvote6)
recode cv10c058 (12=1) (999=0) (nonmissing=0), gen(PVVvote7)
recode cv11d171 (5=1) (999=0) (nonmissing=0), gen(PVVvote8)
recode cv12e171 (5=1) (999=0) (nonmissing=0), gen(PVVvote9)
recode cv13f209 (5=1) (999=0) (nonmissing=0), gen(PVVvote10)
recode cv14g209 (5=1) (999=0) (nonmissing=0), gen(PVVvote11)

recode cv08a058 (7=1) (999=0) (nonmissing=0), gen(GLvote5)
recode cv09b058 (7=1) (999=0) (nonmissing=0), gen(GLvote6)
recode cv10c058 (7=1) (999=0) (nonmissing=0), gen(GLvote7)
recode cv11d171 (9=1) (999=0) (nonmissing=0), gen(GLvote8)
recode cv12e171 (9=1) (999=0) (nonmissing=0), gen(GLvote9)
recode cv13f209 (10=1) (999=0) (nonmissing=0), gen(GLvote10)
recode cv14g209 (10=1) (999=0) (nonmissing=0), gen(GLvote11)

recode cv08a058 (8=1) (999=0) (nonmissing=0), gen(D66vote5)
recode cv09b058 (8=1) (999=0) (nonmissing=0), gen(D66vote6)
recode cv10c058 (8=1) (999=0) (nonmissing=0), gen(D66vote7)
recode cv11d171 (8=1) (999=0) (nonmissing=0), gen(D66vote8)
recode cv12e171 (8=1) (999=0) (nonmissing=0), gen(D66vote9)
recode cv13f209 (8=1) (999=0) (nonmissing=0), gen(D66vote10)
recode cv14g209 (8=1) (999=0) (nonmissing=0), gen(D66vote11)

recode cv08a058 (4=1) (999=0) (nonmissing=0), gen(PvdAvote5)
recode cv09b058 (4=1) (999=0) (nonmissing=0), gen(PvdAvote6)
recode cv10c058 (4=1) (999=0) (nonmissing=0), gen(PvdAvote7)
recode cv11d171 (4=1) (999=0) (nonmissing=0), gen(PvdAvote8)
recode cv12e171 (4=1) (999=0) (nonmissing=0), gen(PvdAvote9)
recode cv13f209 (4=1) (999=0) (nonmissing=0), gen(PvdAvote10)
recode cv14g209 (4=1) (999=0) (nonmissing=0), gen(PvdAvote11)

recode cv08a058 (5=1) (999=0) (nonmissing=0), gen(VVDvote5)
recode cv09b058 (5=1) (999=0) (nonmissing=0), gen(VVDvote6)
recode cv10c058 (5=1) (999=0) (nonmissing=0), gen(VVDvote7)
recode cv11d171 (3=1) (999=0) (nonmissing=0), gen(VVDvote8)
recode cv12e171 (3=1) (999=0) (nonmissing=0), gen(VVDvote9)
recode cv13f209 (3=1) (999=0) (nonmissing=0), gen(VVDvote10)
recode cv14g209 (3=1) (999=0) (nonmissing=0), gen(VVDvote11)

recode cv08a027 cv09b027 cv10c027 cv11d027 cv12e027 cv13f027 cv14g027 (999=.), gen(democr5 democr6 democr7 democr8 democr9 democr10 democr11)

* Fill in missings
foreach var of newlist PVVvote GLvote D66vote PvdAvote VVDvote nativism {
	forvalues wave = 6(1)11 {	
		local l1wave = `wave'-1
		replace `var'`wave' = `var'`l1wave' if `var'`wave' ==.	
		}
	}
pauze

* Regression
* ==========

* Reshaping
reshape long govsat PVVvote nativism democr, i(id) j(panel)
tsset id panel
recode panel (5 = 2007) (6 = 2008) (7 = 2009) (8= 2010) (9 = 2011) (10= 2012) (11 = 2013), gen(year)
recode panel (8 9=1 "Inclusion") (nonmissing=0 "Exclusion"), gen(Wildersgov)

foreach var of varlist nativism PVVvote {
	replace `var'=`var'[_n-1] if `var'>=. & id[_n-1]==id[_n]
	}

* Standardize
egen std_PVVvote = std(PVVvote)
egen std_nativism = std(nativism)
egen std_democr = std(democr)
gen lnativism = l.nativism
gen ldemocr = l.democr

* Descriptives
xtsum democr govsat nativism PVVvote 

* Trend
regress democr i.year##c.nativism
margins, over(year) at(nativism=(0.28 0.5 0.75))
marginsplot, x(year) ytitle("Satisfaction with democracy")  title("Satisfaction with democracy") xtitle("") scheme(plotplainblind) recastci(rarea) ciopts(color(%80))legend(order(1 "Low nativism (5%)" 2 "Average nativism (50%)" 3 "High nativism (95%)")) name(swd, replace) nodraw
regress govsat i.year##c.nativism
margins, over(year) at(nativism=(0.28 0.5 0.75))
marginsplot, x(year) ytitle("Satisfaction with government") title("Satisfaction with government") xtitle("") scheme(plotplainblind) recastci(rarea) ciopts(color(%80)) legend(order(1 "Low nativism (5%)" 2 "Average nativism (50%)" 3 "High nativism (95%)")) name(swg, replace) nodraw
graph combine swd swg, scheme(plottig) xsize(9)
graph export "d:\Dropbox\Inclusion paper\Graphs\Figure A3.png", replace

* Main model
gen lgovsat = l.govsat

xtreg democr ldemocr c.Wildersgov##c.PVVvote c.Wildersgov##c.lnativism, re
estimates store model1
margins, at(Wildersgov=(0 1) lnativism=(0.28 0.5 0.75))
marginsplot, scheme(plotplainblind) recastci(rarea) ciopts(color(%80))  title("") xtitle("") xsize(2) name(graph1, replace) ytitle("Satisfaction with democracy") legend(position(6) order(1 "Low nativism (5%)" 2 "Average nativism (50%)" 3 "High nativism (95%)"))
margins, at(Wildersgov=(0 1) PVVvote=(0 1))
marginsplot, scheme(plotplainblind) recastci(rarea) ciopts(color(%80))  title("") xtitle("") xsize(2) name(graph2, replace) ytitle("Satisfaction with democracy") legend(position(6) order(1 "Non-PVV voter" 2 "PVV voter"))
graph combine graph1 graph2, scheme(plottig) title("Satisfaction with democracy")
graph export "d:\Dropbox\Inclusion paper\Graphs\Figure 5a.png", replace

xtreg govsat lgovsat c.Wildersgov##c.PVVvote c.Wildersgov##c.lnativism, re
estimates store model1
margins, at(Wildersgov=(0 1) lnativism=(0.28 0.5 0.75))
marginsplot, scheme(plotplainblind) recastci(rarea) ciopts(color(%80)) title("") xtitle("") xsize(2) name(graph1, replace) ytitle("Satisfaction with government") legend(position(6) order(1 "Low nativism (5%)" 2 "Average nativism (50%)" 3 "High nativism (95%)"))
margins, at(Wildersgov=(0 1) PVVvote=(0 1))
marginsplot, scheme(plotplainblind) recastci(rarea) ciopts(color(%80)) title("") xtitle("") xsize(2) name(graph2, replace) ytitle("Satisfaction with government") legend(position(6) order(1 "Non-PVV voter" 2 "PVV voter"))
graph combine graph1 graph2, scheme(plottig) title("Satisfaction with government")
graph export "d:\Dropbox\Inclusion paper\Graphs\Figure 5b.png", replace

xtreg democr ldemocr c.Wildersgov c.PVVvote c.lnativism, re
estimates store model1
xtreg democr ldemocr c.Wildersgov##c.PVVvote c.Wildersgov##c.lnativism, re
estimates store model2
xtreg govsat lgovsat c.Wildersgov c.PVVvote c.lnativism, re
estimates store model3
xtreg govsat lgovsat c.Wildersgov##c.PVVvote c.Wildersgov##c.lnativism, re
estimates store model4
estout model1 model2 model3 model4, nobase replace cells(b(star fmt(%9.3f)) se(par(`"="("'`")""'))) stats(R2_a N, fmt(%9.3f %9.0g) labels(R-squared))  ///
	legend label collabels(none) varlabels(_cons Constant)
